{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import urllib\n",
    "import base64\n",
    "import json\n",
    "from IPython.core.display import display, HTML\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def plant(filename,plantnum):\n",
    "    request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/plant\"\n",
    "    \n",
    "    # 二进制方式打开图片文件\n",
    "    f = open(filename, 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = dict()\n",
    "    params['image'] = img\n",
    "    params['baike_num'] = plantnum\n",
    "    params = urllib.parse.urlencode(params).encode(\"utf-8\")\n",
    "    #params = json.dumps(params).encode('utf-8')\n",
    "    \n",
    "    access_token = \"【你的access_token】\"\n",
    "    request_url = request_url + \"?access_token=\" + access_token\n",
    "    request = urllib.request.Request(url=request_url, data=params)\n",
    "    request.add_header('Content-Type', 'application/x-www-form-urlencoded')\n",
    "    response = urllib.request.urlopen(request)\n",
    "    content = response.read()\n",
    "    \n",
    "    if content:\n",
    "        #print(content)\n",
    "        content=content.decode('utf-8')\n",
    "        #print(content)\n",
    "        data = json.loads(content)\n",
    "        result=data['result']        \n",
    "        nums=min(plantnum,len(result))\n",
    "        \n",
    "        for i in range(0,nums):\n",
    "            item=result[i]\n",
    "            print (\"------------------\" + \"识别结果可能为：\" + \"【\" + item['name'] + \"】-----------------------\")            \n",
    "            print ('·名称:',item['name'])\n",
    "            print ('·可能性:',item['score'])            \n",
    "            baike_info=item['baike_info']            \n",
    "            print ('·百科描述：',baike_info['description'])\n",
    "            print (\"\\n\")\n",
    "            print ('·百科链接 =>',baike_info['baike_url'])\n",
    "            print (\"\\n\")\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"./img/3.jpg\" alt=\"\">"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------识别结果可能为：【丹参】-----------------------\n",
      "·名称: 丹参\n",
      "·可能性: 0.8664095401763916\n",
      "·百科描述： 丹参，中药名。为唇形科植物丹参Salvia miltiorrhiza Bge.的干燥根和根茎。春、秋二季采挖，除去泥沙，干燥。全国大部分地区都有分布。具有活血祛瘀，通经止痛，清心除烦，凉血消痈之功效。用于胸痹心痛，脘腹胁痛，症瘕积聚，热痹疼痛，心烦不眠，月经不调，痛经经闭，疮疡肿痛\n",
      "\n",
      "\n",
      "·百科链接 => http://baike.baidu.com/item/%E4%B8%B9%E5%8F%82/53612\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "display(HTML('<img src=\"./img/3.jpg\" alt=\"\">'))\n",
    "plant(\"./img/3.jpg\",1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'log_id': 2204368885978862288, 'result': [{'score': 0.8664095401763916, 'name': '丹参'}, {'score': 0.019449718296527863, 'name': '白花丹参'}, {'score': 0.017086531966924667, 'name': '南丹参'}]}\n"
     ]
    }
   ],
   "source": [
    "# 百度AI开放平台demo\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "植物识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/plant\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('./img/3.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = \"【你的access_token】\"\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
